package com.distributed.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.distributed.common.Address;
import com.distributed.common.Constant;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public final class ConfigLoader {
	public static Map<String, Object> load(String filename) {
		BufferedReader reader = null;
		StringBuilder sb = new StringBuilder();
		try {
			reader = new BufferedReader(new FileReader(filename));
			String tmp = null;
			while((tmp = reader.readLine()) != null) {
				sb.append(tmp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		String tmp = sb.toString();
		Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
		Matcher matcher = pattern.matcher(tmp);
		String config_str = matcher.replaceAll("");
		
		return JsonToMap(config_str);
	}
	
	public static Map<String, Object> JsonToMap(String json) {
		JSONObject jsonobject = null;
		Map<String, Object> config = new HashMap<String, Object>();
		try {
			jsonobject = JSONObject.fromObject(json);
			Iterator<?> iter = jsonobject.keys();
			while(iter.hasNext()) {
				String key = (String) iter.next();
				Object value = jsonobject.get(key);
				if(value instanceof String) {
					config.put(key, value);
				} else if(value instanceof Integer) {
					config.put(key, value);
				} else if(value instanceof JSONArray) {
					JSONArray array = (JSONArray) value;
					List<Address> address_list = new ArrayList<Address>();
					for(int i = 0; i < array.size(); i++) {
						JSONObject obj = (JSONObject) array.get(i);
						Iterator<?> it = obj.keys();
						while (it.hasNext()) {
							String ip = (String) it.next();
							int port = obj.getInt(ip);
							address_list.add(new Address(ip, port));
						}
					}
					config.put(key, address_list);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (NoSuchElementException e1) {
			e1.printStackTrace();
		}
		
		return config;
	}
	
	public static void main(String[] args) {
		Map<String, Object> map = load(Constant.DISP_CONFIG_FILE);
		
		System.out.println(map.get(Constant.DISPATCH_IP));
		System.out.println(map.get(Constant.DISPATCH_PORT));
		Object tmp = map.get(Constant.DSP_LOGIN_SERVER_LIST);
		if(tmp != null && tmp instanceof List<?>) {
			List<?> xtmp = (List<?>) tmp;
			Iterator<?> iter = xtmp.iterator();
			while(iter.hasNext()) {
				Address addr = (Address) iter.next();
				System.out.println(addr.getIp() + ":" + addr.getPort());
			}
		}
	}
}
